/**
 * GameCore
 * @Author Ace.c
 * @Create 2016-12-05 16:08
 */
class game {

    //------------------------微信------------------------
    //TODO 微信相关
    // 纬度，浮点数，范围为90 ~ -90
    static latitude: number = 0;
    // 经度，浮点数，范围为180 ~ -180。
    static longitude: number = 0;
    // 速度，以米/每秒计
    static speed: number = 0;
    // 位置精度
    static accuracy: number = 0;
    // 地理位置参数
    static poswx: string;

    //------------------------显示------------------------

    //分享提示视图
    static share: ShareView;
    //警示提示
    static warning: WarningView;
    //询问提示面板
    static askPanel: TipsAskPanel;
    //键盘
    static keyboard: NumberKeyboard;

    //------------------------数据------------------------

    //解散房间
    static dissolution: DissolutionVo;
    //换三张
    static changeThreeVo: ChangeThreeVo;

    //玩家信息
    static player: PlayerVo;

    //------------------------参数------------------------

    //服务器地址
    static ip: any;
    //服务器端口
    static port: any;
    //用户
    static user: string;
    //分支
    static branch: string;

    //------------------------设置------------------------

    //牌风格
    static desktopColor: number = 0;
    //牌颜色
    static paiColor: number = 0;

    //------------------------游戏------------------------

    //房间-ID
    static roomid: any = 0;
    //房间-规则
    static roomRules: any[] = [];
    //房间-创建时间
    static roomCreateTime: number = 0;
    //规则-回合类型
    static roomRoundType: RoundType = RoundType.quan;
    //规则-回合数
    static roomRound: number = 4;
    //房间-当前局数
    static roomRoundCur: number = 1;
    //规则-牌张(默认13张,特殊规则需要特殊处理,转至RuleConfig)
    static roomPaiZhang: number = 13;
    //规则-座位
    static roomSeat: number = 4;
    //规则-封顶
    static roomSeal: number = 2;
    //房间-玩家
    static roomPlayers: any = {};
    //房间-玩家当前数量
    static roomPlayerCur: number = 0;
    //房间-玩家离线数量
    static roomPlayerOffline: number = 0;
    //用户所在的牌桌位置
    static roomPos: number = 1;

    //房间-玩家换三张记录
    static roomHuan: any = {};
    //房间-玩家缺门记录
    static roomQue: any = {};
    //房间-玩家听牌记录
    static roomTing: any = {};
    //房间-牌堆剩余
    static roomPaidui: number;
    //房间-庄家方向
    static roomZhuangDir: Dir;
    /**房间状态  */
    static roomState: number;


    //是否在更新评价的时候显示评价面板
    static isShowAsscess: boolean = true;
    //是否数据回放中
    static isReplaying: boolean = false;
    //是否是代开
    static isDaikai: boolean = false;
    //是否代开回放
    static ReplayType: number = 0;
    //是否是房主
    static isRoomOwner: boolean = false;
    //是否正在换牌中
    static isChangeThree: boolean = false;
    //是否正在订缺中
    static isQue: boolean = false;
    //是否正在胡牌中
    static isHu: boolean = false;
    //是否禁用按钮
    static isBtnDisabledByTime: boolean = false;

    static isTinging: boolean = false;

    //当前状态
    static status: StateExType = StateExType.gamestart;
    //当前状态是否完成
    static statusComplete: boolean = false;

    //战绩详情用户列表
    static recordInfos: any;
    /**老友圈战绩选中房间信息 */
    static circleRoomInfo: CircleRoomInfo;
    /**当前游戏舞台 */
    public static get curStage() {
        return egret.MainContext.instance.stage;
    }
    /**代开每页显示数量 */
    static Count_Daikai_History_Page: number = 10;
    /**获取当前时间戳*/
    static getTimesamp() {
        return egret.getTimer();
    }
    /**链接中search对象 */
    static search: any;
    // 初始化
    static init() {
        GameStorage.init();
        gameManager.init();
        game.parseLoactionSearch();
        this.share = new ShareView();
        this.warning = new WarningView();
        this.askPanel = new TipsAskPanel();
        this.keyboard = new NumberKeyboard();

        game.player = new PlayerVo();

        game.changeThreeVo = new ChangeThreeVo();

        GSData.i;

        game.desktopColor = +GameStorage.getData(GameStorage.desktopColor);
        game.paiColor = +GameStorage.getData(GameStorage.paiColor);
    }

    //房间准备
    static roomReady() {
        this.roomTing = {};
        this.isHu = false;

        game.dissolution = null;

        gameManager.i.dispatchEvent(EffectEvent.CleanAll);
    }

    //清理房间
    static roomClean() {
        if (!game.isReplaying) game.roomid = 0;
        game.isRoomOwner = false;
        game.roomRules = [];
        game.roomPlayers = {};
        game.roomState = undefined;
    }

    //规则文本
    static ruleTexts(rules: any[] = null): string {
        if (!rules) rules = this.roomRules;

        let text: string = "";
        let rule: any;
        for (let i: number = 0; i < rules.length; i++) {
            rule = rules[i];

            if (typeof rule == "object") {//特殊规则
                switch (rule[0]) {
                    case RuleType.fengding://封顶
                        if (rule[1] >= 99999) {
                            //不显示不封顶
                            if (GameConfig.gameType == GameType.baishan) continue;

                            text += "不封顶 ";
                        }
                        else {
                            game.roomSeal = rule[1];
                            text += rule[1] + "番" + GameRules.ruleText[rule[0]] + " ";
                        }
                        break;
                    case RuleType.paizhang://牌张
                        game.roomPaiZhang = rule[1];
                        break;
                    default:
                        break;
                }
                continue;
            }

            //牌桌不显示听牌规则
            if (rule == RuleType.ting && (GameConfig.gameType == GameType.shenyang || GameConfig.gameType == GameType.jinan)) {
                continue;
            }

            if (rule == RuleType.dianpaobaosanjia && GameConfig.gameType == GameType.chifeng) {
                continue;
            }

            text += GameRules.ruleText[rule] + " ";
        }

        return text;
    }
    /**解析URL参数 */
    static parseLoactionSearch() {
        const params: string = location.search.substring(1);
        let sArr: any[] = params.split("&");
        let circle: string;
        let data: any = {};
        sArr = sArr.map((param) => {
            return param.split("=");
        });

        sArr.forEach((param) => {
            let [key, va] = param;
            data[key] = va;
        });
        game.search = data;
    }
}